import BASE_URL from '@/api/url'
import {groupBy} from '@/lib/js/tools'

//左滑动删除
export const slide = {
    methods: {
        //跳转
        skip: function () {
            if (this.checkSlide()) {
                this.restSlide();
            } else {
                // alert('You click the slide!')
            }
        },
        //滑动开始
        touchStart: function (e) {
            // 记录初始位置
            this.startX = e.touches[0].clientX;
        },
        //滑动结束
        touchEnd: function (e) {
            // 当前滑动的父级元素
            let parentElement = e.currentTarget.parentElement;
            // 记录结束位置
            this.endX = e.changedTouches[0].clientX;
            // 左滑
            if (parentElement.dataset.type == 0 && this.startX - this.endX > 30) {
                this.restSlide();
                parentElement.dataset.type = 1;
            }
            // 右滑
            if (parentElement.dataset.type == 1 && this.startX - this.endX < -30) {
                this.restSlide();
                parentElement.dataset.type = 0;
            }
            this.startX = 0;
            this.endX = 0;
        },
        //判断当前是否有滑块处于滑动状态
        checkSlide: function () {
            let listItems = document.querySelectorAll('.list-item');
            for (let i = 0; i < listItems.length; i++) {
                if (listItems[i].dataset.type == 1) {
                    return true;
                }
            }
            return false;
        },
        //复位滑动状态
        restSlide: function () {
            let listItems = document.querySelectorAll('.list-item');
            // 复位
            for (let i = 0; i < listItems.length; i++) {
                listItems[i].dataset.type = 0;
            }
        },
        //删除
        deleteItem: function (e) {
            // 当前索引
            let index = e.currentTarget.dataset.index;
            // 复位
            this.restSlide();
            // 删除
            this.list.splice(index, 1);
        }
    }
};

//下拉刷新

export const pullDown = {
    methods: {
        loadTop() {

        },

        loadBottom() {
            this.loadMore();
        },
        loadMore() {
            this.pageParam.page++;
            this.$get(this.url, this.pageParam).then(data => {
                let newArr = this.listData.concat(data.data.items);
                this.listData = newArr.map((item) => {
                    return {
                        activityDate: item.activityDate,
                        attachments: !item.attachments ? item.attachments : item.attachments.indexOf('http') > -1 ? item.attachments : `${BASE_URL}/${item.attachments}`,
                        browseCount: item.browseCount,
                        id: item.id,
                        theme: item.theme,
                        description: item.description,
                        praiseCount: item.praiseCount,
                        type: item.type,
                    }
                });
                if (this.$route.query.pageType === 'study') {
                    this.resultData = groupBy(this.listData, (item) => {
                        return item.type
                    })
                }
                !(data.data.totalNum < data.data.currentPage * data.data.pageSize) ? this.allLoaded = false : this.allLoaded = true;
                const obj = {
                    'all': () => {
                        this.$refs.loadmore.onBottomLoaded()
                    },
                    'oncoming': () => {
                        this.$refs.loadmore1.onBottomLoaded()
                    },
                    'past': () => {
                        this.$refs.loadmore2.onBottomLoaded()
                    },
                    '01': () => {
                        this.$refs.loadmore.onBottomLoaded()
                    },
                    '02': () => {
                        this.$refs.loadmore1.onBottomLoaded()
                    },
                    '03': () => {
                        this.$refs.loadmore2.onBottomLoaded()
                    },
                    '04': () => {
                        this.$refs.loadmore3.onBottomLoaded()
                    },
                };
                if (this.pageParam.type in obj) {
                    obj[this.pageParam.type]()
                } else {
                    this.$refs.loadmore.onBottomLoaded()
                }
            });

        },
        //选项卡切换
        tabChange() {
            this.pageParam.page = 1;
            this.allLoaded = false;
            this.pageParam.type = this.selected;
            this.pageParamObj.type = this.selected;
            this.fetchData()
        },
        //跳转对应详情页
        turnToPage(id) {
            this.$router.push({
                name: 'Details',
                query: {
                    id: id,
                    pageType: this.$route.query.pageType,
                }
            })
        },
        // 获取banner
        getBanner() {
            this.$get(`/weixin/banner/${this.pageType}`).then(data=>{
                this.bannerList = data.data.map((item) => {
                    return {
                        activityDate: item.activityDate,
                        attachments: `${BASE_URL}/${item.attachments}`,
                        browseCount: item.browseCount,
                        id: item.id,
                        theme: item.theme,
                        description: item.description,
                        praiseCount: item.praiseCount
                    }
                });

            })
        }

    }

};

